package com.qfedu.mall.common.utils;

import io.jsonwebtoken.*;

import java.util.Date;

public class JwtUtils {

    public static final String secretkey = "QIANfeng6666";


    public static String createToken(String subject,String tokenid){
        JwtBuilder builder = Jwts.builder();

        String token = builder.setSubject(subject)  // 设置主题
                .setIssuedAt(new Date())   // 设置token的创建时间
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60*30)) // 30分种后token过期
                .setId(tokenid)
                .signWith(SignatureAlgorithm.HS256, secretkey)  // 设置生成token 的加密算法
                .compact();
        return token;
    }

    public static boolean parseToken(String token){
        boolean result = true;
        JwtParser parser = Jwts.parser();
        try {
            Jwt jwt = parser.setSigningKey(secretkey)
                    .parse(token);
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }

}
